﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net;
using Lucene.Net.Analysis.PanGu;
using Lucene.Net.Index;
using Lucene.Net.Documents;

namespace RFSerchComponent
{
    public class Index
    {

        public static String INDEX_DIR
        {
            get
            {
                //  return PanGu.Framework.Path.GetAssemblyPath() + @"NewsIndex";
                return "c:\\" + @"NewsIndex";
            }
        }

        private static IndexWriter writer = null;

        public static int MaxMergeFactor
        {
            get
            {
                if (writer != null)
                {
                    return writer.GetMergeFactor();
                }
                else
                {
                    return 0;
                }
            }

            set
            {
                if (writer != null)
                {
                    writer.SetMergeFactor(value);
                }
            }
        }

        public static int MaxMergeDocs
        {
            get
            {
                if (writer != null)
                {
                    return writer.GetMaxMergeDocs();
                }
                else
                {
                    return 0;
                }
            }

            set
            {
                if (writer != null)
                {
                    writer.SetMaxMergeDocs(value);
                }
            }
        }

        public static int MinMergeDocs
        {
            get
            {
                if (writer != null)
                {
                    return writer.GetMaxBufferedDocs();
                }
                else
                {
                    return 0;
                }
            }

            set
            {
                if (writer != null)
                {
                    writer.SetMaxBufferedDocs(value);
                }
            }
        }


        public static void CreateIndex(String indexDir)
        {

            try
            {
                writer = new IndexWriter(indexDir, new PanGuAnalyzer(), false);
            }
            catch
            {
                writer = new IndexWriter(indexDir, new PanGuAnalyzer(), true);
            }

            //writer.Optimize();
            //writer.Close();
        }

        public static void Rebuild(String indexDir)
        {
            writer = new IndexWriter(indexDir, new PanGuAnalyzer(), true);
            writer.Optimize();
            writer.Close();
        }

        public static int IndexString(String indexDir, string url, string title, DateTime time, string content)
        {

            Document doc = new Document();

            Field field = new Field("url", url, Field.Store.YES, Field.Index.NO);
            doc.Add(field);
            field = new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED);
            doc.Add(field);
            field = new Field("time", time.ToString("yyyyMMdd"), Field.Store.YES, Field.Index.UN_TOKENIZED);
            doc.Add(field);
            field = new Field("contents", content, Field.Store.YES, Field.Index.TOKENIZED);
            doc.Add(field);

            writer.AddDocument(doc);

            int num = writer.DocCount();
            //writer.Optimize();
            //writer.Close();
            return num;
        }


        public static int IndexString(string id, string title, string content, string type, string function, string remark, string createTime, string DB)
        {

            Document doc = new Document();

            Field field = new Field("id", id, Field.Store.YES, Field.Index.NO);
            doc.Add(field);
            field = new Field("title", title, Field.Store.NO, Field.Index.TOKENIZED);
            doc.Add(field);
            field = new Field("content", content, Field.Store.NO, Field.Index.TOKENIZED);
            doc.Add(field);
            //field = new Field("abstracts", abstracts, Field.Store.YES, Field.Index.NO);
            //doc.Add(field);
            field = new Field("type", type, Field.Store.YES, Field.Index.UN_TOKENIZED);
            doc.Add(field);
            field = new Field("function", function, Field.Store.YES, Field.Index.UN_TOKENIZED);
            doc.Add(field);
            field = new Field("remark", remark, Field.Store.NO, Field.Index.TOKENIZED);
            doc.Add(field);
            field = new Field("createTime", createTime, Field.Store.YES, Field.Index.UN_TOKENIZED);
            doc.Add(field);
            field = new Field("db", DB, Field.Store.YES, Field.Index.NO);
            doc.Add(field);

            writer.AddDocument(doc);

            int num = writer.DocCount();
            //writer.Optimize();
            //writer.Close();
            return num;
        }
        public static void CloseWithoutOptimize()
        {
            writer.Close();
        }

        public static void Close()
        {
            writer.Optimize();
            writer.Close();
        }


    }
}


